package com.java.web.controller;

import cn.hutool.core.util.StrUtil;
import com.java.common.util.CookieUtils;
import com.java.common.vo.R;
import com.java.member.entity.MyUser;
import com.java.web.feign.MemberFeign;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author jiangli
 * @since 2019/12/11 21:34
 */
@Controller
public class WebController {
	@Autowired
	private MemberFeign memberFeign;

	private static final String COOKIE_NAME = "my_token";

	@GetMapping("/to_login")
	public String tologin() {
		return "login";
	}

	@GetMapping("/to_registry")
	public String registry() {
		return "regist";
	}

	@GetMapping("/index")
	public String index() {
		return "index";
	}

	@RequestMapping("/regist")
	public String registry(MyUser myUser, HttpServletRequest request) {
		R r = memberFeign.registry(myUser);
		Boolean success = (Boolean) r.get("success");
		if (!success) {
			String msg = (String) r.get("msg");
			request.setAttribute("error", msg);
			return "regist";
		}
		return "redirect:login";
	}

	@RequestMapping("/login")
	public String login(MyUser userEntity, HttpServletRequest request, HttpServletResponse response) {
		R r = memberFeign.login(userEntity);
		Boolean success = (Boolean) r.get("success");
		if (!success) {
			String msg = (String) r.get("msg");
			request.setAttribute("error", msg);
			return "login";
		}
		// 登录成功之后,获取token.将token存放在cookie
		String token = (String) r.get("data");
		CookieUtils.setCookie(request, response, COOKIE_NAME, token, 60 * 60 * 2); //单位:秒
		return "index";
	}

	/**
	 * 跳转到QQ授权
	 */
	@GetMapping("/authorizeURL")
	public String authorizeURL(HttpServletRequest request) throws QQConnectException {
		String authorizeURL = new Oauth().getAuthorizeURL(request);
		return "redirect:" + authorizeURL;
	}

	/**
	 * 回调,接收QQ回传参数code
	 */
	@GetMapping("/mycb")
	public String mycb(HttpServletRequest request, HttpServletResponse response) throws QQConnectException {
		//获取accessToken
		AccessToken accessTokenByRequest = new Oauth().getAccessTokenByRequest(request);
		String accessToken = accessTokenByRequest.getAccessToken();
		if (StrUtil.isEmpty(accessToken)) {
			request.setAttribute("error", "QQ授权失败");
			return "login";
		}
		//获取openid
		OpenID openID = new OpenID(accessToken);
		String userOpenID = openID.getUserOpenID();
		UserInfo qzoneUserInfo = new UserInfo(accessToken, userOpenID);
		UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
		String nickname = userInfoBean.getNickname().trim();
		R r = memberFeign.loginByOpenid(userOpenID,nickname);
		Boolean success = (Boolean) r.get("success");
		if (success) {
			String token = (String) r.get("data");
			CookieUtils.setCookie(request, response, COOKIE_NAME, token, 60 * 60 * 2); //单位:秒
			return "redirect:/index";
		}else {
			request.setAttribute("error", "QQ登录失败");
			return "login";
		}
	}
}